package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Double longitude, Double latitude, String addrStr) {

        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
//        需要先判断是否有userLocation记录 如果有修改  如果没有才是新增
//        select * from user_location where userId=??
        if(userLocation==null){
            userLocation = new UserLocation();
            userLocation.setId(new ObjectId());
            userLocation.setUserId(userId);
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
            userLocation.setAddress(addrStr);
            userLocation.setCreated(new Date().getTime());
            userLocation.setUpdated(new Date().getTime());
            userLocation.setLastUpdated(new Date().getTime());
            mongoTemplate.save(userLocation);
        }else{
//            修改哪些字段  location Address  Updated  LastUpdated
            Update update = new Update();
            update.set("location",new GeoJsonPoint(longitude,latitude));
            update.set("address",addrStr);
            update.set("updated",new Date().getTime());
            update.set("lastUpdated",new Date().getTime());
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }

    }

    @Override
    public List<UserLocationVo> search(Long userId, Integer distance) {
//        distance 单位是米 换成公里  distance/1000
//        以当前登录人位置为圆心，以distance为半径 画圆圈，在圆圈之内的用户就是我们想要的人
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint point = userLocation.getLocation();

        Circle circle = new Circle(point, new Distance(distance / 1000, Metrics.KILOMETERS));
        Query query1 = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(query1, UserLocation.class);
        return UserLocationVo.formatToList(userLocationList);
    }


    /**
     * 根据用户id查询获取用户位置
     *
     * @param userId
     * @return
     */
    @Override
    public UserLocationVo queryUserLocation(Long userId) {
        //根据userId查询UserLocation表
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        UserLocationVo userLocationVo = UserLocationVo.format(userLocation);
        return userLocationVo;
    }
}
